package com.gexin.controller.system.recommended;

import com.gexin.common.model.ApiResponse;
import com.gexin.core.web.SystemBaseController;
import com.gexin.model.CashCouponLog;
import com.gexin.model.MemberIntegral;
import com.gexin.model.activity.*;
import com.gexin.model.order.Order;
import com.gexin.model.order.OrderItem;
import com.gexin.model.weixin.WeixinUser;
import com.jfinal.ext.route.ControllerBind;
import org.apache.commons.collections4.CollectionUtils;

import javax.servlet.http.Cookie;
import java.math.BigDecimal;
import java.net.HttpCookie;
import java.util.*;
import java.util.stream.Collectors;

@ControllerBind(controllerKey = "/system/recommended", viewPath = "WEB-INF/system/team")
public class RecommendedController extends SystemBaseController {

    public void index(){
        render("add.html");
    }

    public void OneQueryteam(){
        String id=getPara("id");
        WeixinUser weixinUser=WeixinUser.dao.onecWeixinUser(id);
        Integer userid=weixinUser.getId();
        MemberBillSpeedIntegral memberBillSpeedIntegral=MemberBillSpeedIntegral.dao.onceMemberBillSpeedIntegral(userid);
        BigDecimal speedIntegral = memberBillSpeedIntegral.getSpeedIntegral();
        InviteeRelative inviteeRelative=InviteeRelative.dao.getQueryteam(userid);
        String InviterId=inviteeRelative.getInviterId();
        InviteeRelative inviteeRelative3=InviteeRelative.dao.findFirst("select user_id from t_invitee_relative where id=?",InviterId);
        String userPareId="";
        if(inviteeRelative3!=null){
            userPareId=inviteeRelative3.getUserId();
        }
        WeixinUser weixinUser1=WeixinUser.dao.findFirst("select nickname from t_weixin_user where id=?",userPareId);
        String userPareName="无";
        if(weixinUser1!=null){
            userPareName=weixinUser1.getNickname();
        }
        String OneId=inviteeRelative.getId();
        String UserId=inviteeRelative.getUserId();
        Long CretateTime=inviteeRelative.getCretateTime();
        WeixinUser weixinUserList=WeixinUser.dao.oneWeixinUser(UserId);
        MemberBillSpeedIntegral memberBillSpeedIntegralList1=MemberBillSpeedIntegral.dao.findFirst("select daily_increment from t_member_bill_speed_integral where user_id="+UserId);
        BigDecimal dailyIncrement = BigDecimal.ZERO;
        if(memberBillSpeedIntegralList1!=null){
            dailyIncrement = memberBillSpeedIntegralList1.getDailyIncrement() == null ? BigDecimal.ZERO : memberBillSpeedIntegralList1.getDailyIncrement();
        }
        List<InviteeRelative> inviteeRelative1=InviteeRelative.dao.allInviteeRelativeList(OneId);
        long count = 0;
        if(CollectionUtils.isNotEmpty(inviteeRelative1)){
            count = inviteeRelative1.get(0).get("count(*)");
        }
        List<InviteeRelative> inviteeRelative2=InviteeRelative.dao.getCount(OneId);
        long crowdordering=0;
        if(CollectionUtils.isNotEmpty(inviteeRelative2)){
            crowdordering = inviteeRelative2.get(0).get("count(*)");
        }
        List<GroupRule> groupRule=GroupRule.dao.find("SELECT level FROM t_group_rule WHERE number_of_people<="+count+" and number_of_people_max>"+count+"");
        Integer level = 0;
        if(CollectionUtils.isNotEmpty(groupRule)){
            level = groupRule.get(0).getLevel();
        }
        Map map = new HashMap();
        map.put("user",weixinUserList.getNickname());
        map.put("speedIntegral",dailyIncrement);
        map.put("level",level);
        map.put("CretateTime",CretateTime);
        map.put("UserId",UserId);
        map.put("count",count);
        map.put("crowdordering",crowdordering);
        map.put("speedIntegral",speedIntegral);
        map.put("userPareName",userPareName);
        renderJson(new ApiResponse(true,"返回成功",map));
        return;
    }

    public void Queryteam(){
        List<Object> list= new ArrayList<>();
        Integer id=getParaToInt("id");
        InviteeRelative inviteeRelative=InviteeRelative.dao.getQueryteam(id);
        String OneId=inviteeRelative.getId();
        List<InviteeRelative> inviteeRelativeList=InviteeRelative.dao.getQueryteamList(OneId);
        for(int i=0;i<inviteeRelativeList.size();i++){
            String OneUserId=inviteeRelativeList.get(i).getUserId();
            String TowId=inviteeRelativeList.get(i).getId();
            Long CretateTime=inviteeRelativeList.get(i).getCretateTime();
            String userId=inviteeRelativeList.get(i).getUserId();
            String PareId=inviteeRelativeList.get(i).getInviterId();

            InviteeRelative inviteeRelative3=InviteeRelative.dao.findFirst("select user_id from t_invitee_relative where id=?",PareId);
            String userPareId="";
            if(inviteeRelative3!=null){
                userPareId=inviteeRelative3.getUserId();
            }
            WeixinUser weixinUser1=WeixinUser.dao.findFirst("select nickname from t_weixin_user where id=?",userPareId);
            String userPareName="无";
            if(weixinUser1!=null){
                userPareName=weixinUser1.getNickname();
            }

//        	用户名
            WeixinUser weixinUserList=WeixinUser.dao.oneWeixinUser(OneUserId);
//        	增值积分
            MemberBillSpeedIntegral memberBillSpeedIntegral=MemberBillSpeedIntegral.dao.
                    findFirst("select daily_increment from t_member_bill_speed_integral where user_id="+OneUserId);
            BigDecimal dailyIncrement = BigDecimal.ZERO;
            if(memberBillSpeedIntegral!=null){
                dailyIncrement = memberBillSpeedIntegral.getDailyIncrement() == null ? BigDecimal.ZERO : memberBillSpeedIntegral.getDailyIncrement();
            }
            List<InviteeRelative> inviteeRelativeList1=InviteeRelative.dao.allInviteeRelativeList(TowId);
            long count1 = 0;
            if(CollectionUtils.isNotEmpty(inviteeRelativeList1)){
                count1 = inviteeRelativeList1.get(0).get("count(*)")==null?0:inviteeRelativeList1.get(0).get("count(*)");
            }
            List<InviteeRelative> inviteeRelative2=InviteeRelative.dao.getCount(TowId);
            long crowdordering=0;
            if(CollectionUtils.isNotEmpty(inviteeRelative2)){
                crowdordering = inviteeRelative2.get(0).get("count(*)");
            }
            MemberBillSpeedIntegral memberBillSpeedIntegral1=MemberBillSpeedIntegral.dao.onceeMemberBillSpeedIntegral(TowId);
            BigDecimal speedIntegral = BigDecimal.ZERO;
            if(memberBillSpeedIntegral1!=null){
                speedIntegral = memberBillSpeedIntegral1.getSpeedIntegral() == null ? BigDecimal.ZERO : memberBillSpeedIntegral1.getSpeedIntegral();
            }
//			团队等级
            List<GroupRule> groupRulelist=GroupRule.dao.find("SELECT level FROM t_group_rule WHERE number_of_people<="+count1+" and number_of_people_max>"+count1+"");
            Integer level = 0;
            if(CollectionUtils.isNotEmpty(groupRulelist)){
                level = groupRulelist.get(0).getLevel();
            }
            Map map = new HashMap();
            map.put("user",weixinUserList.getNickname());
            map.put("speedIntegral",dailyIncrement);
            map.put("level",level);
            map.put("CretateTime",CretateTime);
            map.put("UserId",userId);
            map.put("count",count1);
            map.put("crowdordering",crowdordering);
            map.put("speedIntegral",speedIntegral);
            map.put("userPareName",userPareName);
            list.add(map);
        }
        renderJson(new ApiResponse(true,"返回成功",list));
        System.out.println(list);
        return;
    }


    public void getCashCouponLog(){
        List<CashCouponLog> cashCouponLogList=CashCouponLog.dao.allCashCoupon();
    }

    public void TheOriginalIntegral1(){
        Integer id=getParaToInt("id");
        List<Order> orderList=Order.dao.getOrderList(id);
        List<MemberOrder> memberOrderList=MemberOrder.dao.find("select order_id,acquired_integral,bill_integral,deliver_multiple,forecast_integral from t_member_order");
        Map<String, MemberOrder> memberOrderMap = memberOrderList.stream().filter(memberOrder -> memberOrder.getOrderId() != null).
                collect(Collectors.toMap(MemberOrder::getOrderId, a -> a, (k1, k2) -> k1));
//		orderList = orderList.stream().filter(order -> memberOrderMap.get(order.getId() + "") != null).collect(Collectors.toList());
//		查询所有订单详情,转变成map(根据orderid)
        List<OrderItem> orderItemList=OrderItem.dao.find("select order_id,nums from t_order_item");
        List<OrderItem> orderItems=orderItemList.stream().filter(orderItem ->orderItem!=null
                &&memberOrderMap.get(orderItem.getOrderId())!=null).collect(Collectors.toList());
        System.out.println(orderList);
        System.out.println(memberOrderList);
        System.out.println(orderItems);
    }

    //details.html
    public void details(){
        render("details.html");
    }

    //原始积分
    public void TheOriginalIntegral(){
        Integer id=getParaToInt("id");
        List<Order> orderList=Order.dao.getOrderList(id);
        List<Object> list=new ArrayList();
        for(int i=0;i<orderList.size();i++){
            String orderId=orderList.get(i).getId();
            MemberOrder memberOrder=MemberOrder.dao.findFirst("select * from t_member_order where order_id="+orderId);
            BigDecimal DeliverMultiple=BigDecimal.ZERO;
            BigDecimal BillIntegral=BigDecimal.ZERO;
            BigDecimal ForecastIntegral=BigDecimal.ZERO;
            if(memberOrder!=null){
                DeliverMultiple=memberOrder.getDeliverMultiple() == null ? BigDecimal.ZERO : memberOrder.getDeliverMultiple();
                BillIntegral=memberOrder.getBillIntegral() == null ? BigDecimal.ZERO : memberOrder.getBillIntegral();
                ForecastIntegral=memberOrder.getForecastIntegral() == null ? BigDecimal.ZERO : memberOrder.getForecastIntegral();
            }
            Date CreateTime= orderList.get(i).getCreatetime();
            OrderItem orderItems=OrderItem.dao.findFirst("select nums from t_order_item where order_id="+orderId);
            BigDecimal nums = BigDecimal.ZERO;
            if(orderItems!=null){
                nums = orderItems.getNums() == null ? BigDecimal.ZERO : BigDecimal.valueOf(orderItems.getNums());
            }
            String OrderProductName=orderList.get(i).getOrderProductName();
            if(OrderProductName==null){
                OrderProductName="";
            }
            Map map = new HashMap();
            //订单id
            map.put("id",orderList.get(i).getId());
            //商品名称
            map.put("order_product_name",OrderProductName);
            //订单总金额
            map.put("total_amount",orderList.get(i).getTotalAmount());
            //订单商品数量
            map.put("nums",nums);
            //释放倍数
            map.put("DeliverMultiple",DeliverMultiple);
            //原始积分
            map.put("BillIntegral",BillIntegral);
            //预计可获得
            map.put("ForecastIntegral",ForecastIntegral);
            //交易时间
            map.put("CreateTime",CreateTime);
            list.add(map);
        }
        renderJson(new ApiResponse(true,"返回成功",list));
        System.out.println(list);
        return;
    }


    //金币
    public void UserCoin(){
        String id=getPara("id");
        List<MemberCoinLog> memberCoinLog=MemberCoinLog.dao.find("select * from t_member_coin_log where user_id="+id);
        renderJson(new ApiResponse(true,"返回成功",memberCoinLog));
        System.out.println(memberCoinLog);
        return;
    }


    public void UserDeliveryVolume(){
        String id=getPara("id");
        List<MemberIntegralLog> memberIntegralLogList=MemberIntegralLog.dao.getmemberIntegralLogList(id);
        List<MemberCashCouponLog> memberCashCouponLogList=MemberCashCouponLog.dao.findByUserId(id);
        System.out.println("memberIntegralLogList:"+memberIntegralLogList);
        System.out.println("memberCashCouponLogList:"+memberCashCouponLogList);
        renderJson(new ApiResponse());
    }


    public void userCoinCertificate(){
        String id=getPara("id");
        List<MemberCashCouponLog> memberCashCouponLogList=MemberCashCouponLog.dao.findByUserId(id);
        renderJson(new ApiResponse(true,"返回成功",memberCashCouponLogList));
        System.out.println(memberCashCouponLogList);
        return;
    }
}